using System;
using System.Configuration;
using System.Data;
using digit.Core;



namespace SMS.ServiceManager
{
    public class PlayerManager
    {
        #region CONSTRUCTORS
        static PlayerManager()
        {
            _data = new PlayerData(Util.GetConnectionString("SMSSERVICE"));
            _error = "";
        }
        #endregion

        #region ATTRIBUTES
        private static PlayerData _data;
        private static string _error;
        #endregion

        #region PROPERTIES
        public static string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        #endregion

        #region METHODS
        public static bool Insert(string playerName, string birthday, string weight, string height, string shirtNo, string playPosition, string imgPath, string birthPlace, string archieve, string pastTeam, string countryName, string hireDate, string info, string createdBy, DateTime createdDate, byte type, byte status)
        {
            _error = "";
            try
            {
                object[] values = new object[17];
                values[0] = Util.ReplaceNullTo(playerName, DBNull.Value);
                values[1] = Util.ReplaceNullTo(birthday, DBNull.Value);
                values[2] = Util.ReplaceNullTo(weight, DBNull.Value);
                values[3] = Util.ReplaceNullTo(height, DBNull.Value);
                values[4] = Util.ReplaceNullTo(shirtNo, DBNull.Value);
                values[5] = Util.ReplaceNullTo(playPosition, DBNull.Value);
                values[6] = Util.ReplaceNullTo(imgPath, DBNull.Value);
                values[7] = Util.ReplaceNullTo(birthPlace, DBNull.Value);
                values[8] = Util.ReplaceNullTo(archieve, DBNull.Value);
                values[9] = Util.ReplaceNullTo(pastTeam, DBNull.Value);
                values[10] = Util.ReplaceNullTo(countryName, DBNull.Value);
                values[11] = Util.ReplaceNullTo(hireDate, DBNull.Value);
                values[12] = Util.ReplaceNullTo(info, DBNull.Value);
                values[13] = Util.ReplaceNullTo(createdBy, DBNull.Value);
                values[14] = Util.ReplaceNullTo(createdDate, DBNull.Value);
                values[15] = Util.ReplaceNullTo(type, DBNull.Value);
                values[16] = Util.ReplaceNullTo(status, DBNull.Value);

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Insert(PlayerInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[17];
                values[0] = Util.ReplaceNullTo(info.PlayerName, DBNull.Value);
                values[1] = Util.ReplaceNullTo(info.Birthday, DBNull.Value);
                values[2] = Util.ReplaceNullTo(info.Weight, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.Height, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.ShirtNo, DBNull.Value);
                values[5] = Util.ReplaceNullTo(info.PlayPosition, DBNull.Value);
                values[6] = Util.ReplaceNullTo(info.ImgPath, DBNull.Value);
                values[7] = Util.ReplaceNullTo(info.BirthPlace, DBNull.Value);
                values[8] = Util.ReplaceNullTo(info.Archieve, DBNull.Value);
                values[9] = Util.ReplaceNullTo(info.PastTeam, DBNull.Value);
                values[10] = Util.ReplaceNullTo(info.CountryName, DBNull.Value);
                values[11] = Util.ReplaceNullTo(info.HireDate, DBNull.Value);
                values[12] = Util.ReplaceNullTo(info.Info, DBNull.Value);
                values[13] = Util.ReplaceNullTo(info.CreatedBy, DBNull.Value);
                values[14] = Util.ReplaceNullTo(info.CreatedDate, DBNull.Value);
                values[15] = Util.ReplaceNullTo(info.Type, DBNull.Value);
                values[16] = Util.ReplaceNullTo(info.Status, DBNull.Value);

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(long playerID, string playerName, string birthday, string weight, string height, string shirtNo, string playPosition, string imgPath, string birthPlace, string archieve, string pastTeam, string countryName, string hireDate, string info, string createdBy, DateTime createdDate, byte type, byte status)
        {
            _error = "";
            try
            {
                object[] values = new object[18];
                values[0] = playerID;
                values[1] = Util.ReplaceNullTo(playerName, DBNull.Value);
                values[2] = Util.ReplaceNullTo(birthday, DBNull.Value);
                values[3] = Util.ReplaceNullTo(weight, DBNull.Value);
                values[4] = Util.ReplaceNullTo(height, DBNull.Value);
                values[5] = Util.ReplaceNullTo(shirtNo, DBNull.Value);
                values[6] = Util.ReplaceNullTo(playPosition, DBNull.Value);
                values[7] = Util.ReplaceNullTo(imgPath, DBNull.Value);
                values[8] = Util.ReplaceNullTo(birthPlace, DBNull.Value);
                values[9] = Util.ReplaceNullTo(archieve, DBNull.Value);
                values[10] = Util.ReplaceNullTo(pastTeam, DBNull.Value);
                values[11] = Util.ReplaceNullTo(countryName, DBNull.Value);
                values[12] = Util.ReplaceNullTo(hireDate, DBNull.Value);
                values[13] = Util.ReplaceNullTo(info, DBNull.Value);
                values[14] = Util.ReplaceNullTo(createdBy, DBNull.Value);
                values[15] = Util.ReplaceNullTo(createdDate, DBNull.Value);
                values[16] = Util.ReplaceNullTo(type, DBNull.Value);
                values[17] = Util.ReplaceNullTo(status, DBNull.Value);

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(PlayerInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[18];
                values[0] = info.PlayerID;
                values[1] = Util.ReplaceNullTo(info.PlayerName, DBNull.Value);
                values[2] = Util.ReplaceNullTo(info.Birthday, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.Weight, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.Height, DBNull.Value);
                values[5] = Util.ReplaceNullTo(info.ShirtNo, DBNull.Value);
                values[6] = Util.ReplaceNullTo(info.PlayPosition, DBNull.Value);
                values[7] = Util.ReplaceNullTo(info.ImgPath, DBNull.Value);
                values[8] = Util.ReplaceNullTo(info.BirthPlace, DBNull.Value);
                values[9] = Util.ReplaceNullTo(info.Archieve, DBNull.Value);
                values[10] = Util.ReplaceNullTo(info.PastTeam, DBNull.Value);
                values[11] = Util.ReplaceNullTo(info.CountryName, DBNull.Value);
                values[12] = Util.ReplaceNullTo(info.HireDate, DBNull.Value);
                values[13] = Util.ReplaceNullTo(info.Info, DBNull.Value);
                values[14] = Util.ReplaceNullTo(info.CreatedBy, DBNull.Value);
                values[15] = Util.ReplaceNullTo(info.CreatedDate, DBNull.Value);
                values[16] = Util.ReplaceNullTo(info.Type, DBNull.Value);
                values[17] = Util.ReplaceNullTo(info.Status, DBNull.Value);

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(long playerID)
        {
            _error = "";
            try
            {
                object[] values = { playerID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(PlayerInfo info)
        {
            _error = "";
            try
            {
                object[] values = { info.PlayerID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static PlayerInfo Select(long playerID)
        {
            _error = "";
            try
            {
                object[] values = { playerID };
                DataTable dTable = _data.Select(values);
                if (dTable.Rows.Count < 1) return null;


                PlayerInfo info = new PlayerInfo(dTable.Rows[0]);
                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAll()
        {
            _error = "";
            try
            {
                return _data.SelectAll();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool UpdateStatus(int playerId, byte bytStatus)
        {
            _error = "";
            try
            {
                object[] values = new object[2];
                values[0] = playerId;
                values[1] = bytStatus;
                return _data.UpdateStatus(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static DataTable SelectAllForRelate(byte bytStatus)
        {
            _error = "";
            try
            {
                return _data.SelectAllForRelate(bytStatus);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectTop()
        {
            _error = "";
            try
            {
                return _data.SelectTop();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectVisible(int page)
        {
            _error = "";
            try
            {
                return _data.SelectVisible(page * 15, 16);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectStar(int page)
        {
            _error = "";
            try
            {
                return _data.SelectStar(page * 25, 26);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAllByTeam(long lngTeam)
        {
            _error = "";
            try
            {
                return _data.SelectAllByTeam(lngTeam);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectOtherInTeam(long lngTeam, long lngPlayerID)
        {
            _error = "";
            try
            {
                object[] values = new object[2];
                values[0] = lngTeam;
                values[1] = lngPlayerID;
                return _data.SelectOtherInTeam(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        #endregion

    }
}